define(['jquery','common'], function($,common){
    var captcha = {
        //获取动态验证码
        get: function(element){
            $(document).on("click", element, function(){
                var thisClick = $(this);
                //验证码类型(mobile|email)
                var captchaType = thisClick.data('type');
                //目标元素
                var targetId = thisClick.data('target');
                //手机号或邮箱
                var targetVal  = $(targetId).val();
                if(!targetVal){
                    $(targetId).focus();
                    return false;
                }
                //验证图形码值
                var captchaImage = $(thisClick.data('captcha')+">input").val();
                if(!captchaImage){
                    $(thisClick.data('captcha')+">input").focus();
                    return false;
                }
                //不同验证码类型
                if(captchaType == 'mobile'){
                    //验证手机号
                    if(!common.isMobile(targetVal)){
                        common.toastMini('请输入正确的手机号');
                        return false;
                    }
                    //发送短信验证码
                    captcha.ajax($(this).data('url'),{'mobile': targetVal,'captcha':captchaImage},$(this));
                }else{
                    //验证邮箱
                    if(!common.isEmail(targetVal)){
                        common.toastMini('请输入正确的邮箱');
                        return false;
                    }
                    //发送邮箱验证码
                    captcha.ajax($(this).data('url'),{'email': targetVal,'captcha':captchaImage},$(this));
                }
            });
        },
        //后端请求
        ajax: function(url, data, $el){
            $.post(url, data, function(res){
                if(res.code == 1){
                    captcha.interval($el);
                }else{
                    captcha.imageRefresh($el.data('captcha')+">>img");
                    captcha.imageValue($el.data('captcha')+">input",null);
                    common.toastMini(res.msg);
                }
            });
        },
        //按钮倒计时
        interval: function($selector){
            var second = 60;
            $selector.addClass('disabled');
            $selector.text(second+'秒后重发');
            var interval = setInterval(function () {
                second--;
                $selector.text(second+'秒后重发');
                if(second < 1){
                    $selector.text($selector.data('reset'));
                    $selector.removeClass('disabled');
                    clearInterval(interval);
                }
            }, 1000);
        },
        //监听图形验证码
        image: function(element){
            $(element).css({cursor:'pointer'});
            $(document).on("click", element, function(){
                captcha.imageRefresh(element);
            });
        },
        //图形验证码输入框值
        imageValue:function(element,value){
            $(element).val(value);
        },
        //刷新图形验证码
        imageRefresh: function(element){
            var imgUrl = $(element).attr('src');
            $(element).attr('src', imgUrl);
        }
    };
    return captcha;
});